<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class system extends Admin_Controller{
    function __construct() {
        parent::__construct();
        if(!$this->session->userdata('username')){
            redirect('login');
        }
        
        $this->load->model('admin_model');
	    $this->load->model('admin_group_model');
        $this->load->model('admin_log_model');
        $this->load->model('menu_model');
        $this->load->model('tree_model');
        $this->load->model('cate_model');
        $this->load->model('dictionary_model');
        $username = $this->session->userdata('username');
        
    }
    
    function index(){
        $listtree = $this->menu_model->getlist(1);
        
        foreach($listtree as $k=>$val)
        {
            $listtree[$k]['son'] = $this->menu_model->getlist($val['id']);

            foreach($listtree[$k]['son'] as $k1=>$val01)
            {
                $listtree[$k]['son'][$k1]['son'] = $this->menu_model->getlist($val01['id']);

                foreach($listtree[$k]['son'][$k1]['son'] as $k2=>$val02)
                {
                $listtree[$k]['son'][$k1]['son'][$k2]['son'] = $this->menu_model->getlist($val02['id']);

                }

            }

        }

        //p($listtree);die;
        //重定义页面
        $this->template->assign('segment_p','menu');
        $this->template->assign('listtree',$listtree);
        $this->template->load('menu/menu_front');
        $this->template->load('system/menu_front');
    }
    //前台菜单管理
    function menu(){
		
        $listtree = $this->menu_model->getlist(1);
        
        foreach($listtree as $k=>$val)
        {
            $listtree[$k]['son'] = $this->menu_model->getlist($val['id']);

            foreach($listtree[$k]['son'] as $k1=>$val01)
            {
                $listtree[$k]['son'][$k1]['son'] = $this->menu_model->getlist($val01['id']);

                foreach($listtree[$k]['son'][$k1]['son'] as $k2=>$val02)
                {
                $listtree[$k]['son'][$k1]['son'][$k2]['son'] = $this->menu_model->getlist($val02['id']);

                }

            }

        }

        //p($listtree);die;
        //重定义页面
        $this->template->assign('segment_p','menu');
        $this->template->assign('listtree',$listtree);
        $this->template->load('menu/menu_front');
    }
    
     //后台菜单管理
    function menu_backend(){
        $listtree = $this->menu_model->getlist(2);
			
        foreach($listtree as $k=>$val)
        {
            $listtree[$k]['son'] = $this->menu_model->getlist($val['id']);

            foreach($listtree[$k]['son'] as $k1=>$val01)
            {
                $listtree[$k]['son'][$k1]['son'] = $this->menu_model->getlist($val01['id']);

                foreach($listtree[$k]['son'][$k1]['son'] as $k2=>$val02)
                {
                $listtree[$k]['son'][$k1]['son'][$k2]['son'] = $this->menu_model->getlist($val02['id']);

                }

            }

        }
        //重定义页面
        $this->template->assign('segment_p','menu');
        $this->template->assign('listtree',$listtree);
        $this->template->load('menu/menu_backend');
    }
    
    //添加系统菜单
    function menu_add(){
        if($_POST){
            $mssage = array();
            $title	= trim($_POST['title']);
            $sort	= trim($_POST['sort']);
            $url	= trim($_POST['url']);
            $pid	= trim($_POST['pid']);
            $group	= trim($_POST['group']);
            $hide	= trim($_POST['hide']);
            $is_dev	= trim($_POST['is_dev']);
            $tip	= trim($_POST['tip']);

            if(!$url){
                $mssage['info']	  = 'url必须填写';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                //echo json_encode($mssage);exit;
            }
            if(!$title){
                $mssage['info']	  = '名称必须填写';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
            $data = array();
            $data['pid']	= $pid;
            $data['title']	= $title;
            $data['sort']	= $sort;
            $data['url']	= $url;
            $data['group']	= $group;
            $data['hide']	= $hide;
            $data['is_dev']	= $is_dev;
            $data['tip']	= $tip;

            //$result = $this->tree_model->addCategory($pid,$title,$url,$sort,$display);
            $result = $this->tree_model->addCategory($data);

            if($result){
                //更新菜单缓存
                update_cache('menu');
                $mssage['info']	  = '添加成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url('system/menu_add').'?pid='.$pid;
            }else{
                $mssage['info']	  = '添加不成功';
                $mssage['status'] = 0;
                $mssage['url']	  = site_url('system/menu_add').'?pid='.$pid;
            }

            echo json_encode($mssage);exit;
        }
        $pid = $_GET['pid'] ? $_GET['pid'] : '2';
        $station=$this->tree_model->getOptionStr($pid,true,5,false,0);
        $this->template->assign('station',$station);
        $this->template->assign('xpid',$pid);
        //重定义页面
        $this->template->assign('segment_p','menu');
        $this->template->load('menu/menu_add');
    }
    //菜单快速编辑
    function menu_edit()
    {	
        if($_POST){
            $mssage = array();
            $id	   = trim($_POST['id']);
            $title = trim($_POST['title']);
            $sort  = trim($_POST['sort']);
            $url   = trim($_POST['url']);

            if(!$id){
                    $mssage['info']	  = '系统错误';
                    $mssage['status'] = 0;
                    $mssage['url']	  = '';
                    echo json_encode($mssage);exit;
            }
            if(!$url){
                    $mssage['info']	  = 'url必须填写';
                    $mssage['status'] = 0;
                    $mssage['url']	  = '';
                    //echo json_encode($mssage);exit;
            }
            if(!$title){
                    $mssage['info']	  = '名称必须填写';
                    $mssage['status'] = 0;
                    $mssage['url']	  = '';
                    echo json_encode($mssage);exit;
            }

            $data = array();
            $data['url'] = $url;
            $data['title'] = $title;
            $data['sort'] = $sort;

            $this->db->where('id', $id);
            $this->db->update('menu',$data);
            if($this->db->affected_rows()){
                update_cache('menu');
                $mssage['info']	  = '修改成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url('system/menu');
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '修改失败';
                $mssage['status'] = 0;
                $mssage['url']	  = site_url('system/menu');
                echo json_encode($mssage);exit;
            }
        }

    }
    //修改系统菜单
     function menu_medit(){
         if($_POST){
            $mssage = array();
            $id		= trim($_POST['id']);
            $title	= trim($_POST['title']);
            $sort	= trim($_POST['sort']);
            $url	= trim($_POST['url']);
            $pid	= trim($_POST['pid']);
            $group	= trim($_POST['group']);
            $hide	= trim($_POST['hide']);
            $is_dev	= trim($_POST['is_dev']);
            $tip	= trim($_POST['tip']);

            if(!$url){
                $mssage['info']	  = 'url必须填写';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                //echo json_encode($mssage);exit;
            }
            if(!$title){
                $mssage['info']	  = '名称必须填写';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
            $data = array();
            $data['id']		= $id;
            $data['pid']	= $pid;
            $data['title']	= $title;
            $data['sort']	= $sort;
            $data['url']	= $url;
            $data['group']	= $group;
            $data['hide']	= $hide;
            $data['is_dev']	= $is_dev;
            $data['tip']	= $tip;

            $result = $this->tree_model->editCategory($data);

            if($result){
                //更新菜单缓存
                update_cache('menu');
                $mssage['info']	  = '修改成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url('system/menu_medit').'?id='.$id;
            }else{
                $mssage['info']	  = '修改不成功';
                $mssage['status'] = 0;
                $mssage['url']	  = site_url('system/menu_medit').'?id='.$id;
            }
            echo json_encode($mssage);exit;
        }
         //------------------
        $id = $_GET['id'];
        $query = $this->tree_model->fetchOne($id);
        $pid = $query['pid'];
        $station=$this->tree_model->getOptionStr(0,true,5,false,$pid);
        $this->template->assign('station',$station);
        $this->template->assign('item',$query);
        //重定义页面
        $this->template->assign('segment_p','menu');
        $this->template->load('menu/menu_edit');
     }
     //是否隐藏菜单
    function menu_setstatus()
    {	
        if($_GET){
            $mssage = array();
            $id	   = trim($_GET['ids']);
            $status = trim($_GET['status']);
            $data = array();
            $data['hide'] = $status;
            $this->db->where('id', $id);
            $this->db->update('menu',$data);
            if($this->db->affected_rows()){
                update_cache('menu');
                if($status==1){
                    $mssage['info']	  = '禁用成功';
                }else{
                    $mssage['info']	  = '启用成功';
                }
                $mssage['status'] = 1;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '操作失败';
                $mssage['status'] = 0;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }

        }	

    }
    
    public function menu_remove()
	{
            $id	   = intval($_GET['id']);
            if(!$id){
                redirect('system/menu');
            }

            $this->db->where('id', $id);
            $this->db->delete('menu');
            if($this->db->affected_rows()){
                    update_cache('menu');
                    $mssage['info']	  = '删除成功';
                    $mssage['status'] = 1;
                    $mssage['url']	  = site_url('system/menu');
                    echo json_encode($mssage);exit;
           }else{
                    $mssage['info']	  = '删除失败';
                    $mssage['status'] = 0;
                    $mssage['url']	  = site_url('system/menu');
                    echo json_encode($mssage);exit;
           }
	
	}
    
    //系统用户管理
    function employee(){
        
        $where = array();
        $where['role'] = 'admin';
        //分页
        $getcount = $this->admin_model->get_count($where);

        $navigation_position = 'outside';
        $records_per_page = 10;
        $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position, array('left', 'right')) ? $navigation_position : 'outside');
        $this->xpage->records($getcount);
        $this->xpage->records_per_page($records_per_page);
        $this->xpage->labels("<<", ">>");
        $userlist = $this->admin_model->get_list($where, (($this->xpage->get_page() - 1) * $records_per_page) ,$records_per_page);
        
        foreach($userlist as $key=>$vo){
            $state = '';
            if($vo['state']=='1'){
                $state = '<span class="red">正常</span>';
            }else{
                $state = '<span class="red">禁用</span>';
            }
            $userlist[$key]['username'] = $vo['username'];
            $userlist[$key]['truename'] = $vo['truename'];
            $userlist[$key]['state']	 = $state;
            $userlist[$key]['mobile']	 = $vo['mobile'];
        }
        //重定义页面
        $this->template->assign('segment_p','employee');
        $this->template->assign('Total',$getcount);
        $this->template->assign('showpage',$this->xpage->render(true));
        $this->template->assign('list',$userlist);
        $this->template->load('system/employee');
    }
    
    //增加系统管理用户
    function employee_add(){
        if($_POST){
            $data = array();
            if(!trim($_POST['username'])){
                $mssage['info']	  = '用户名必须输入';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
            
            if($this->admin_model->Get(trim($_POST['username']))){
                $mssage['info']	  = '用户名已经存在';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
            if(!trim($_POST['password'])){
                $mssage['info']	  = '密码必须输入';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
            if(!trim($_POST['group_id'])){
                $mssage['info']	  = '请选择用户组';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
            if(!trim($_POST['truename'])){
                $mssage['info']	  = '用户姓名必须填写';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
            if(!trim($_POST['mobile'])){
                $mssage['info']	  = '手机号码必须填写';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
            
            $data['username'] = trim($_POST['username']);
            $data['password'] = md5(trim($_POST['username']).md5(trim($_POST['password'])));
            $data['truename'] = trim($_POST['truename']);
            $data['group_id'] = trim($_POST['group_id']);
            $data['mobile'] = trim($_POST['mobile']);
            $data['qq'] = trim($_POST['qq']);
            $data['email'] = trim($_POST['email']);
            $data['state'] = trim($_POST['state']);
            $data['add_time'] = time();
            $data['role']   	= 'admin';
            $result = $this->admin_model->insert($data);
            if($result){
                $logdata = array();
                $logdata['user_id'] = $this->userinfo['id'];
                $logdata['username'] = $this->userinfo['username'];
                $logdata['event'] = 'system';
                $logdata['content'] = '增加管理员：'.$_POST['username'].'-'.$_POST['truename'];
                $this->admin_log_model->insert($logdata);


                $mssage['info']	  = '增加成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url('system/employee');
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '注册不成功';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
            
        }
        $group_select = $this->admin_group_model->group_select('');
        $this->template->assign('group_select',$group_select);
        $this->template->assign('segment_p','employee');
        $this->template->load('system/employee_add');
    }
    //增加系统管理用户
    function employee_edit(){
        $id = intval($_GET['id']);
        if(!$id){
            redirect('system/employee');
        }
        
        if($_POST){
            $id = trim($_POST['id']);
            $data = array();
            if(!trim($_POST['username'])){
                $mssage['info']	  = '用户名必须输入';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
            
            
            
            if(trim($_POST['username']) != trim($_POST['rusername']) && $this->admin_model->Get(trim($_POST['username']))){
                $mssage['info']	  = '用户名已经存在';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
            if(!trim($_POST['group_id'])){
                $mssage['info']	  = '请选择用户组';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
            if(!trim($_POST['truename'])){
                $mssage['info']	  = '用户姓名必须填写';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
            if(!trim($_POST['mobile'])){
                $mssage['info']	  = '手机号码必须填写';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
            $data['username'] = trim($_POST['username']);
            if($_POST['password']){
                $data['password'] = md5(trim($_POST['username']).md5(trim($_POST['password'])));
            }
            $data['truename'] = trim($_POST['truename']);
            $data['group_id'] = trim($_POST['group_id']);
            $data['mobile'] = trim($_POST['mobile']);
            $data['qq'] = trim($_POST['qq']);
            $data['email'] = trim($_POST['email']);
            $data['state'] = trim($_POST['state']);
            $data['update_time'] = time();
            $result = $this->admin_model->userupdate($data,$id);
            if($result){
                $logdata = array();
                $logdata['user_id'] = $this->userinfo['id'];
                $logdata['username'] = $this->userinfo['username'];
                $logdata['event'] = 'system';
                $logdata['content'] = '修改管理员：'.$_POST['username'].'-'.$_POST['truename'];
                $this->admin_log_model->insert($logdata);


                $mssage['info']	  = '修改成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url('system/employee');
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '操作失败';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
        }
        
        $item = $this->admin_model->Get($id);
        $group_select = $this->admin_group_model->group_select($item['group_id']);
        $this->template->assign('group_select',$group_select);
        //重定义页面
        $this->template->assign('segment_p','employee');
        $this->template->assign('item',$item);
        $this->template->load('system/employee_edit');
    }
    //删除管理员
    function employee_del()
	{
            $id	   = intval($_GET['id']);
            if(!$id){
                redirect('system/employee');
            }

            $result = $this->admin_model->delete($id);
            if($result){
                $logdata = array();
                $logdata['user_id'] = $this->userinfo['id'];
                $logdata['username'] = $this->userinfo['username'];
                $logdata['event'] = 'system';
                $logdata['content'] = '删除管理员：【'.$_GET['name'].'】';
                $this->admin_log_model->insert($logdata);

                $mssage['info']	  = '删除成功';
                $mssage['status'] = 1;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '删除失败';
                $mssage['status'] = 0;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }
	
	}
    
    //权限管理组
    function permission(){
        $where = array();
        $getcount = $this->admin_group_model->get_count($where);
        $list = $this->admin_group_model->GetAll($where);
        $this->template->assign('segment_p','permission');
        $this->template->assign('Total',$getcount);
        $this->template->assign('list',$list);
        $this->template->load('system/permission');
    }
    
    //增加管理权限组
    function permission_add(){
        if($_POST){
            if(trim($_POST['name'])==''){
                $mssage['info']	  = '分组名称不能为空';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
            $data = array();
            $data['group_name'] = trim($_POST['name']);
            $data['description'] = trim($_POST['description']);
            $data['status'] = trim($_POST['status']);
            $data['sort'] = trim($_POST['sort']);
            $data['create_time'] = time();
            $result = $this->admin_group_model->insert($data);
            if($result){
				    
                $logdata = array();
                $logdata['user_id'] = $this->userinfo['id'];
                $logdata['username'] = $this->userinfo['username'];
                $logdata['event'] = 'system';
                $logdata['content'] = '增加用户组：【'.$data['group_name'].'】';
                $this->admin_log_model->insert($logdata);

                $mssage['info']	  = '添加成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url('system/permission');
                echo json_encode($mssage);exit;
           }else{
                $mssage['info']	  = '操作失败';
                $mssage['status'] = 0;
                $mssage['url']	  = site_url('system/permission');
                echo json_encode($mssage);exit;
           }
        }
        //重定义页面
        $this->template->assign('segment_p','permission');
        $this->template->load('system/permission_add');
    }
    
    //修改管理权限组
    function permission_edit(){
        $id = intval($_GET['id']);
        if(!$id){
            redirect('system/permission');
        }
        if($_POST){
            $id = $_POST['id'];
            
            $data = array();
            
            $data['group_name'] = trim($_POST['name']);
            $data['description'] = trim($_POST['description']);
            $data['status'] = trim($_POST['status']);
            $data['sort'] = trim($_POST['sort']);
            $data['update_time'] = time();
            if(trim($_POST['name'])==''){
                $mssage['info']	  = '分组名称不能为空';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }

            $result = $this->admin_group_model->update($data,$id);
            if($result){
                $logdata = array();
                $logdata['user_id'] = $this->userinfo['id'];
                $logdata['username'] = $this->userinfo['username'];
                $logdata['event'] = 'system';
                $logdata['content'] = '修改用户组：【'.$data['group_name'].'】';
                $this->admin_log_model->insert($logdata);

                $mssage['info']	  = '修改成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url('system/permission');
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '操作失败';
                $mssage['status'] = 0;
                $mssage['url']	  = site_url('system/permission');
                echo json_encode($mssage);exit;
            }

        }
        $item = $this->admin_group_model->Get($id);
        //重定义页面
        $this->template->assign('segment_p','permission');
        $this->template->assign('item',$item);
        $this->template->load('system/permission_edit');
    }
    
    //删除管理权限用户组
    function permission_del(){
        $id = intval($_GET['id']);
        $group_name = trim($_GET['name']);
        if(!$id){
            redirect('system/permission');
        }
        $result = $this->admin_group_model->delete($id);
        if($result){
                $logdata = array();
                $logdata['user_id'] = $this->userinfo['id'];
                $logdata['username'] = $this->userinfo['username'];
                $logdata['event'] = 'system';
                $logdata['content'] = '删除用户组：【'.$group_name.'】';
                $this->admin_log_model->insert($logdata);

                $mssage['info']	  = '删除成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url('system/permission');
                echo json_encode($mssage);exit;
        }else{
                $mssage['info']	  = '删除失败';
                $mssage['status'] = 0;
                $mssage['url']	  = site_url('system/permission');
                echo json_encode($mssage);exit;
        }
    }
    //设置管理权限用户组启用/禁止状态
    function permission_setstatus(){
        if($_GET){
            $mssage = array();
            $id	= trim($_GET['ids']);
            $status = trim($_GET['status']);
            $group_name = trim($_GET['name']);
            $data = array();
            $data['status'] = $status;
            $data['update_time'] = time();
            $this->admin_group_model->update($data,$id);
            if($this->db->affected_rows()){
                if($status==0){
                     $mssage['info']	  = '禁用成功';

                     $logdata = array();
                     $logdata['user_id'] = $this->userinfo['id'];
                     $logdata['username'] = $this->userinfo['username'];
                     $logdata['event'] = 'system';
                     $logdata['content'] = '禁用用户组：【'.$group_name.'】';
                     $this->admin_log_model->insert($logdata);

                }else{
                     $mssage['info']	  = '启用成功';

                     $logdata = array();
                     $logdata['user_id'] = $this->userinfo['id'];
                     $logdata['username'] = $this->userinfo['username'];
                     $logdata['event'] = 'system';
                     $logdata['content'] = '启用用户组：【'.$group_name.'】';
                     $this->admin_log_model->insert($logdata);

                }
                $mssage['status'] = 1;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '操作失败';
                $mssage['status'] = 0;
                $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                echo json_encode($mssage);exit;
            }

        }
    }
    
    //给权限用户组分配访问权限
    function permission_access(){
        $id = intval($_GET['id']);
        if(!$id){
            redirect('system/permission');
        }
        if($_POST){
            $mssage = array();
            $ids = $_POST['rules'];
            $access = implode(',',$ids);
            $data = array();
            $id	= intval($_POST['id']);
            if(!$id){
                redirect('system/permission');
            }

            $data['access'] = $access;
            $data['update_time'] = time();
            $result = $this->admin_group_model->update($data,$id);
            if($result){

                $logdata = array();
                $logdata['user_id'] = $this->userinfo['id'];
                $logdata['username'] = $this->userinfo['username'];
                $logdata['event'] = 'system';
                $logdata['content'] = '权限分配给：【'.$_POST['name'].'】，权限：'.$access;
                $this->admin_log_model->insert($logdata);

                $mssage['info']	  = '授权成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url('system/permission');
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '授权失败';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
				
	}
        
        $item = $this->admin_group_model->Get($id);
        $item['access'] = explode(',',$item['access']);
        $this->template->assign('item',$item);
        //重定义页面
        $this->template->assign('segment_p','permission');
        $this->template->load('system/permission_access');
    }
    //商品分类管理
    function giftcategory(){
        $listtree = $this->cate_model->getgiftlist(0);
        foreach($listtree as $k=>$val)
        {
            $listtree[$k]['son'] = $this->cate_model->getgiftlist($val['id']);
            foreach($listtree[$k]['son'] as $k1=>$val01)
            {
                $listtree[$k]['son'][$k1]['son'] = $this->cate_model->getgiftlist($val01['id']);
            }
        }
        //重定义页面
        $this->template->assign('segment_p','giftcategory');
        $this->template->assign('listtree',$listtree);
        $this->template->load('system/giftcategory');
    }
    
    function giftcategory_medit()
	{	
		
		
		if($_POST){
			$mssage = array();
			$id		= trim($_POST['id']);
			$title	= trim($_POST['title']);
			$sort	= trim($_POST['sort']);
			$pid	= trim($_POST['pid']);
			$hide	= trim($_POST['hide']);
			$group	= trim($_POST['group']);
			if(!$title){
                            $mssage['info']	  = '名称必须填写';
                            $mssage['status'] = 0;
                            $mssage['url']	  = '';
                            echo json_encode($mssage);exit;
			}
			
			
			$data = array();
			$data['id']		= $id;
			$data['pid']	= $pid;
			$data['title']	= $title;
			$data['sort']	= $sort;
			$data['hide']	= $hide;
			$data['group']	= $group;
			$result = $this->cate_model->gift_update($data,$id);
			
			if($result){
				//update_cache('category');
				$logdata = array();
				$logdata['user_id'] = $this->userinfo['id'];
				$logdata['username'] = $this->userinfo['username'];
				$logdata['event'] = 'system';
				$logdata['content'] = '修改分类：'.$title;
				$this->admin_log_model->insert($logdata);
				
				$mssage['info']	  = '修改成功';
				$mssage['status'] = 1;
				$mssage['url']	  = site_url('system/giftcategory');
			}else{
				$mssage['info']	  = '修改不成功';
				$mssage['status'] = 0;
				$mssage['url']	  = site_url('system/giftcategory');
			}
			
			echo json_encode($mssage);exit;
		
		}
		//------------------
		$id = $_GET['id'];
		$item = $this->cate_model->get_gift_data($id);
		$station=$this->cate_model->tree($this->cate_model->GetGiftAll());
//                p($station);die;
		$this->template->assign('station',$station);
		$this->template->assign('item',$item);
		//重定义页面
		$this->template->assign('segment_p','giftcategory');
		$this->template->load('system/giftcategory_edit');
	}
        //新增商品分类
        function giftcategory_add(){
            if($_POST){
			$mssage = array();
			$title	= trim($_POST['title']);
			$sort	= trim($_POST['sort']);
			$pid	= trim($_POST['pid']);
			$hide	= trim($_POST['hide']);
			$group	= trim($_POST['group']);
			
			if(!$title){
				$mssage['info']	  = '名称必须填写';
				$mssage['status'] = 0;
				$mssage['url']	  = '';
				echo json_encode($mssage);exit;
			}
			
			
			$data = array();
			$data['pid']	= $pid;
			$data['title']	= $title;
			$data['sort']	= $sort;
			$data['hide']	= $hide;
			$data['group']	= $group;
			$result = $this->cate_model->gift_insert($data);
			
			if($result){
				//update_cache('category');
				
				$logdata = array();
				$logdata['user_id'] = $this->userinfo['id'];
				$logdata['username'] = $this->userinfo['username'];
				$logdata['event'] = 'system';
				$logdata['content'] = '新增分类：'.$title;
				$this->admin_log_model->insert($logdata);
				
				$mssage['info']	  = '添加成功';
				$mssage['status'] = 1;
				$mssage['url']	  = site_url('system/giftcategory');
			}else{
				$mssage['info']	  = '添加不成功';
				$mssage['status'] = 0;
				$mssage['url']	  = site_url('system/giftcategory');
			}
			
			echo json_encode($mssage);exit;
		
		}
                
		$cate_id = $this->input->get('pid')? $this->input->get('pid') : 0;
		$this->template->assign('cate_id',$cate_id);
		//重定义页面
		$this->template->assign('segment_p','giftcategory');
		$station=$this->cate_model->tree($this->cate_model->GetGiftAll());
		$this->template->assign('station',$station);
		
		$this->template->load('system/giftcategory_add');
        }
        public function giftcategory_edit()
	{	
		
		if($_POST){
			$mssage = array();
			
			$id	   = trim($_POST['id']);
			$title = trim($_POST['title']);
			$sort  = trim($_POST['sort']);
			$group	= trim($_POST['group']);
			if(!$id){
				$mssage['info']	  = '系统错误';
				$mssage['status'] = 0;
				$mssage['url']	  = '';
				echo json_encode($mssage);exit;
			}
			if(!$title){
				$mssage['info']	  = '名称必须填写';
				$mssage['status'] = 0;
				$mssage['url']	  = '';
				echo json_encode($mssage);exit;
			}
			
			$data = array();
			$data['title'] = $title;
			$data['sort'] = $sort;
			$data['group'] = $group;
			$result = $this->cate_model->gift_update($data,$id);
			   if($result){
					//update_cache('menu');
					$logdata = array();
					$logdata['user_id'] = $this->userinfo['id'];
					$logdata['username'] = $this->userinfo['username'];
					$logdata['event'] = 'system';
					$logdata['content'] = '修改分类：'.$title;
					$this->admin_log_model->insert($logdata);
				
				    $mssage['info']	  = '修改成功';
					$mssage['status'] = 1;
					$mssage['url']	  = site_url('system/category');
					echo json_encode($mssage);exit;
			   }else{
				    $mssage['info']	  = '修改失败';
					$mssage['status'] = 0;
					$mssage['url']	  = site_url('system/category');
					echo json_encode($mssage);exit;
			   }
	   
			
			
			
		}
	
	}
        //商品类型删除
        function giftcategory_remove(){
            $id	   = intval($_GET['id']);
            if(!$id){
                redirect('system/giftcategory');
            }

            $result = $this->cate_model->gift_delete($id);
            if($result){

                    $logdata = array();
                    $logdata['user_id'] = $this->userinfo['id'];
                    $logdata['username'] = $this->userinfo['username'];
                    $logdata['event'] = 'system';
                    $logdata['content'] = '删除分类：'.$id;
                    $this->admin_log_model->insert($logdata);

                    $mssage['info']	  = '删除成功';
                    $mssage['status'] = 1;
                    $mssage['url']	  = site_url('system/giftcategory');
                    echo json_encode($mssage);exit;
            }else{
                    $mssage['info']	  = '删除失败';
                    $mssage['status'] = 0;
                    $mssage['url']	  = site_url('system/giftcategory');
                    echo json_encode($mssage);exit;
            }
        }
        
        function giftcategory_setstatus()
	{	
		
		if($_GET){
			$mssage = array();
			$id	   = trim($_GET['ids']);
			$status = trim($_GET['status']);
			$data = array();
			$data['hide'] = $status;
			$result = $this->cate_model->gift_update($data,$id);
			   if($result){
				    if($status==1){
					$mssage['info']	  = '禁用成功';
					
					$logdata = array();
					$logdata['user_id'] = $this->userinfo['id'];
					$logdata['username'] = $this->userinfo['username'];
					$logdata['event'] = 'system';
					$logdata['content'] = '禁用分类：'.$id;
					$this->admin_log_model->insert($logdata);
					
					}else{
					$mssage['info']	  = '启用成功';
					
					$logdata = array();
					$logdata['user_id'] = $this->userinfo['id'];
					$logdata['username'] = $this->userinfo['username'];
					$logdata['event'] = 'system';
					$logdata['content'] = '启用分类：'.$id;
					$this->admin_log_model->insert($logdata);
					
					}
					$mssage['status'] = 1;
					$mssage['url']	  = site_url('system/giftcategory');
					echo json_encode($mssage);exit;
			   }else{
				    $mssage['info']	  = '操作失败';
					$mssage['status'] = 0;
					$mssage['url']	  = site_url('system/giftcategory');
					echo json_encode($mssage);exit;
			   }
			   
		}	
	
	}
        
        //内容分类管理
    function contentcategory(){
        $listtree = $this->cate_model->getcontentlist(0);
        foreach($listtree as $k=>$val)
        {
            $listtree[$k]['son'] = $this->cate_model->getcontentlist($val['id']);
            foreach($listtree[$k]['son'] as $k1=>$val01)
            {
                $listtree[$k]['son'][$k1]['son'] = $this->cate_model->getcontentlist($val01['id']);
            }
        }
        //重定义页面
        $this->template->assign('segment_p','contentcategory');
        $this->template->assign('listtree',$listtree);
        $this->template->load('system/contentcategory');
    }
    
    function contentcategory_medit()
	{	
		
		
		if($_POST){
			$mssage = array();
			$id		= trim($_POST['id']);
			$title	= trim($_POST['title']);
                        $code	= trim($_POST['code']);
			$sort	= trim($_POST['sort']);
			$pid	= trim($_POST['pid']);
			$hide	= trim($_POST['hide']);
			$group	= trim($_POST['group']);
			if(!$title){
                            $mssage['info']	  = '名称必须填写';
                            $mssage['status'] = 0;
                            $mssage['url']	  = '';
                            echo json_encode($mssage);exit;
			}
			
			
			$data = array();
			$data['id']		= $id;
			$data['pid']	= $pid;
			$data['title']	= $title;
                        $data['code'] = $code;
			$data['sort']	= $sort;
			$data['hide']	= $hide;
			$data['group']	= $group;
			$result = $this->cate_model->content_update($data,$id);
			
			if($result){
				//update_cache('category');
				$logdata = array();
				$logdata['user_id'] = $this->userinfo['id'];
				$logdata['username'] = $this->userinfo['username'];
				$logdata['event'] = 'system';
				$logdata['content'] = '修改分类：'.$title;
				$this->admin_log_model->insert($logdata);
				
				$mssage['info']	  = '修改成功';
				$mssage['status'] = 1;
				$mssage['url']	  = site_url('system/contentcategory');
			}else{
				$mssage['info']	  = '修改不成功';
				$mssage['status'] = 0;
				$mssage['url']	  = site_url('system/contentcategory');
			}
			
			echo json_encode($mssage);exit;
		
		}
		//------------------
		$id = $_GET['id'];
		$item = $this->cate_model->get_content_data($id);
		$station=$this->cate_model->tree($this->cate_model->GetContentAll());
//                p($station);die;
		$this->template->assign('station',$station);
		$this->template->assign('item',$item);
		//重定义页面
		$this->template->assign('segment_p','contentcategory');
		$this->template->load('system/contentcategory_edit');
	}
        //新增商品分类
        function contentcategory_add(){
            if($_POST){
			$mssage = array();
			$title	= trim($_POST['title']);
			$sort	= trim($_POST['sort']);
			$pid	= trim($_POST['pid']);
                        $code	= trim($_POST['code']);
			$hide	= trim($_POST['hide']);
			$group	= trim($_POST['group']);
			
			if(!$title){
				$mssage['info']	  = '名称必须填写';
				$mssage['status'] = 0;
				$mssage['url']	  = '';
				echo json_encode($mssage);exit;
			}
			
			
			$data = array();
			$data['pid']	= $pid;
			$data['title']	= $title;
                        $data['code'] = $code;
			$data['sort']	= $sort;
			$data['hide']	= $hide;
			$data['group']	= $group;
			$result = $this->cate_model->content_insert($data);
			
			if($result){
				//update_cache('category');
				
				$logdata = array();
				$logdata['user_id'] = $this->userinfo['id'];
				$logdata['username'] = $this->userinfo['username'];
				$logdata['event'] = 'system';
				$logdata['content'] = '新增分类：'.$title;
				$this->admin_log_model->insert($logdata);
				
				$mssage['info']	  = '添加成功';
				$mssage['status'] = 1;
				$mssage['url']	  = site_url('system/contentcategory');
			}else{
				$mssage['info']	  = '添加不成功';
				$mssage['status'] = 0;
				$mssage['url']	  = site_url('system/contentcategory');
			}
			
			echo json_encode($mssage);exit;
		
		}
                
		$cate_id = $this->input->get('pid')? $this->input->get('pid') : 0;
		$this->template->assign('cate_id',$cate_id);
		//重定义页面
		$this->template->assign('segment_p','contentcategory');
		$station=$this->cate_model->tree($this->cate_model->GetContentAll());
		$this->template->assign('station',$station);
		
		$this->template->load('system/contentcategory_add');
        }
        public function contentcategory_edit()
	{	
		
		if($_POST){
			$mssage = array();
			
			$id	   = trim($_POST['id']);
			$title = trim($_POST['title']);
                        $code	= trim($_POST['code']);
			$sort  = trim($_POST['sort']);
			$group	= trim($_POST['group']);
			if(!$id){
				$mssage['info']	  = '系统错误';
				$mssage['status'] = 0;
				$mssage['url']	  = '';
				echo json_encode($mssage);exit;
			}
			if(!$title){
				$mssage['info']	  = '名称必须填写';
				$mssage['status'] = 0;
				$mssage['url']	  = '';
				echo json_encode($mssage);exit;
			}
			
			$data = array();
			$data['title'] = $title;
                        $data['code'] = $code;
			$data['sort'] = $sort;
			$data['group'] = $group;
			$result = $this->cate_model->content_update($data,$id);
			   if($result){
					//update_cache('menu');
					$logdata = array();
					$logdata['user_id'] = $this->userinfo['id'];
					$logdata['username'] = $this->userinfo['username'];
					$logdata['event'] = 'system';
					$logdata['content'] = '修改分类：'.$title;
					$this->admin_log_model->insert($logdata);
				
				    $mssage['info']	  = '修改成功';
					$mssage['status'] = 1;
					$mssage['url']	  = site_url('system/contentcategory');
					echo json_encode($mssage);exit;
			   }else{
				    $mssage['info']	  = '修改失败';
					$mssage['status'] = 0;
					$mssage['url']	  = site_url('system/contentcategory');
					echo json_encode($mssage);exit;
			   }
	   
			
			
			
		}
	
	}
        //商品类型删除
        function contentcategory_remove(){
            $id	   = intval($_GET['id']);
            if(!$id){
                redirect('system/giftcategory');
            }

            $result = $this->cate_model->content_delete($id);
            if($result){

                    $logdata = array();
                    $logdata['user_id'] = $this->userinfo['id'];
                    $logdata['username'] = $this->userinfo['username'];
                    $logdata['event'] = 'system';
                    $logdata['content'] = '删除分类：'.$id;
                    $this->admin_log_model->insert($logdata);

                    $mssage['info']	  = '删除成功';
                    $mssage['status'] = 1;
                    $mssage['url']	  = site_url('system/contentcategory');
                    echo json_encode($mssage);exit;
            }else{
                    $mssage['info']	  = '删除失败';
                    $mssage['status'] = 0;
                    $mssage['url']	  = site_url('system/contentcategory');
                    echo json_encode($mssage);exit;
            }
        }
        
        function contentcategory_setstatus()
	{	
		
		if($_GET){
			$mssage = array();
			$id	   = trim($_GET['ids']);
			$status = trim($_GET['status']);
			$data = array();
			$data['hide'] = $status;
			$result = $this->cate_model->content_update($data,$id);
			   if($result){
				    if($status==1){
					$mssage['info']	  = '禁用成功';
					
					$logdata = array();
					$logdata['user_id'] = $this->userinfo['id'];
					$logdata['username'] = $this->userinfo['username'];
					$logdata['event'] = 'system';
					$logdata['content'] = '禁用分类：'.$id;
					$this->admin_log_model->insert($logdata);
					
					}else{
					$mssage['info']	  = '启用成功';
					
					$logdata = array();
					$logdata['user_id'] = $this->userinfo['id'];
					$logdata['username'] = $this->userinfo['username'];
					$logdata['event'] = 'system';
					$logdata['content'] = '启用分类：'.$id;
					$this->admin_log_model->insert($logdata);
					
					}
					$mssage['status'] = 1;
					$mssage['url']	  = site_url('system/contentcategory');
					echo json_encode($mssage);exit;
			   }else{
				    $mssage['info']	  = '操作失败';
					$mssage['status'] = 0;
					$mssage['url']	  = site_url('system/contentcategory');
					echo json_encode($mssage);exit;
			   }
			   
		}	
	
	}
     //网站配置   
    function webconfig(){
        if($_POST){
            $data = $_POST;
            if($this->db->where("id",1)->update("tan_website_config",$data)){
                $mssage['info']	  = '更新成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url('system/webconfig');
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '更新失败';
                $mssage['status'] = 0;
                $mssage['url']	  = site_url('system/webconfig');
                echo json_encode($mssage);exit;
            }
        }
        $item = $this->db->query("select * from tan_website_config where id = 1 order by id desc limit 1")->row_array();
        $this->template->assign('item',$item);
        //重定义页面
        $this->template->assign('segment_p','webconfig');
        $this->template->load('system/webconfig');
    }
    //字典管理
    function dictionary(){

        $where = array();
        //分页
        $getcount = $this->dictionary_model->get_count($where);

        $navigation_position = 'outside';
        $records_per_page = 10;
        $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position, array('left', 'right')) ? $navigation_position : 'outside');
        $this->xpage->records($getcount);
        $this->xpage->records_per_page($records_per_page);
        $this->xpage->labels("<<", ">>");
        $dictlist = $this->dictionary_model->get_list($where, (($this->xpage->get_page() - 1) * $records_per_page) ,$records_per_page);

        foreach($dictlist as $key=>$vo){
            switch ($vo['type']){
                case 1:
                    $dictlist[$key]['type'] = "请求来源";
                    break;
                case 2:
                    $dictlist[$key]['type'] = "请求flag";
                    break;
                case 3:
                    $dictlist[$key]['type'] = "任务标签";
                    break;
            }
            $dictlist[$key]['name'] = $vo['name'];

        }
        //重定义页面
        $this->template->assign('Total',$getcount);
        $this->template->assign('showpage',$this->xpage->render(true));
        $this->template->assign('list',$dictlist);
        $this->template->load('system/dictionary');
    }
    //增加字典
    function dictionary_add(){
        if($_POST){
            $data = array();
            if(!trim($_POST['name'])){
                $mssage['info']	  = '名称必须输入';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }

            if($this->dictionary_model->get_re(array("type"=>trim($_POST['type']),"name"=>trim($_POST['name'])))){
                $mssage['info']	  = '同类型中名称已经存在';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
            if($this->dictionary_model->get_re(array("type"=>trim($_POST['type']),"value"=>trim($_POST['value'])))){
                $mssage['info']	  = '同类型中字典值已经存在';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }

            if(!trim($_POST['type'])){
                $mssage['info']	  = '请选择字典类型';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }


            $data['name'] = trim($_POST['name']);
            $data['value'] = trim($_POST['value']);
            $data['type'] = trim($_POST['type']);
            $result = $this->dictionary_model->insert($data);
            if($result){
                $logdata = array();
                $logdata['user_id'] = $this->userinfo['id'];
                $logdata['username'] = $this->userinfo['username'];
                $logdata['event'] = 'system';
                $logdata['content'] = '增加字典：'.$_POST['name'].'-'.$_POST['value'];
                $this->admin_log_model->insert($logdata);


                $mssage['info']	  = '增加成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url('system/dictionary');
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '新增不成功';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }

        }
        $this->template->assign('act_t','新增');
        $this->template->load('system/dictionary_info');
    }
    //编辑字典
    function dictionary_edit(){
        $id = intval($_GET['id']);
        if(!$id){
            redirect('dictionary/index');
        }

        if($_POST){
            $id = trim($_POST['id']);
            $data = array();
            if(!trim($_POST['name'])){
                $mssage['info']	  = '名称必须输入';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }

            if($this->dictionary_model->get_re(array("type"=>trim($_POST['type']),"name"=>trim($_POST['name']),"id !="=>$id))){
                $mssage['info']	  = '同类型中名称已经存在';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
            if($this->dictionary_model->get_re(array("type"=>trim($_POST['type']),"value"=>trim($_POST['value']),"id !="=>$id))){
                $mssage['info']	  = '同类型中字典值已经存在';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
            if(!trim($_POST['type'])){
                $mssage['info']	  = '请选择字典类型';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
            if(!trim($_POST['value'])){
                $mssage['info']	  = '字典值必须填写';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }

            $data['name'] = trim($_POST['name']);
            $data['type'] = trim($_POST['type']);
            $data['value'] = trim($_POST['value']);

            $result = $this->dictionary_model->update($data,$id);
            if($result){
                $logdata = array();
                $logdata['user_id'] = $this->userinfo['id'];
                $logdata['username'] = $this->userinfo['username'];
                $logdata['event'] = 'system';
                $logdata['content'] = '修改字典：'.$_POST['name'].'-'.$_POST['value'];
                $this->admin_log_model->insert($logdata);


                $mssage['info']	  = '修改成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url('system/dictionary');
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '操作失败';
                $mssage['status'] = 0;
                $mssage['url']	  = '';
                echo json_encode($mssage);exit;
            }
        }

        $item = $this->dictionary_model->Get($id);

        $this->template->assign('act_t','修改');
        $this->template->assign('item',$item);
        $this->template->load('system/dictionary_info');
    }
    //删除字典
    function dictionary_del()
    {
        $id	   = intval($_GET['id']);
        if(!$id){
            redirect('system/dictionary');
        }

        $result = $this->dictionary_model->delete($id);
        if($result){
            $logdata = array();
            $logdata['user_id'] = $this->userinfo['id'];
            $logdata['username'] = $this->userinfo['username'];
            $logdata['event'] = 'dictionary';
            $logdata['content'] = '删除字典：【'.$_GET['name'].'】';
            $this->admin_log_model->insert($logdata);

            $mssage['info']	  = '删除成功';
            $mssage['status'] = 1;
            $mssage['url']	  = $_SERVER['HTTP_REFERER'];
            echo json_encode($mssage);exit;
        }else{
            $mssage['info']	  = '删除失败';
            $mssage['status'] = 0;
            $mssage['url']	  = $_SERVER['HTTP_REFERER'];
            echo json_encode($mssage);exit;
        }

    }



    //app版本控制
    function appversion(){
        
        
        $where = array();
        //总数
        $total = $this->db->select("*")->from("version")->where($where)->order_by("id desc")->get()->num_rows();
        $navigation_position = 'outside';
        $records_per_page = 10;
        $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position,
            array('left', 'right')) ? $navigation_position : 'outside');
        //$this->xpage->reverse(true);
        $this->xpage->records($total);
        $this->xpage->records_per_page($records_per_page);
        $this->xpage->labels("<<", ">>");
        //记录
        $list = $this->db->select("*")->from("version")->where($where)->order_by("id desc")->limit($records_per_page,
            (($this->xpage->get_page() - 1) * $records_per_page))->get()->result_array();
        $this->template->assign('Total',$total);
        $this->template->assign('list',$list);
        $this->template->assign('segment_p','appversion');
        $this->template->load('system/appversion');
    }
    
    function appversion_add(){
        
        if($_POST){
            $data = array();
            $data['type'] = $_POST['type'];
            $data['app_type'] = $_POST['app_type'];
            $data['version'] = $_POST['version'];
            $data['url'] = $_POST['url'];
            $data['isupdate'] = $_POST['isupdate'];
            $data['content'] = $_POST['content'];
            
            if(!$data['app_type']){
                $mssage['info']	  = '请选择【应用App类型】';
                $mssage['status'] = 0;
                $mssage['url']	  = "";
                echo json_encode($mssage);exit;
            }
            if(!$data['type']){
                $mssage['info']	  = '请选择【操作系统类型】';
                $mssage['status'] = 0;
                $mssage['url']	  = "";
                echo json_encode($mssage);exit;
            }
            
            if($this->db->insert("tan_version",$data)){
                $mssage['info']	  = '添加成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url('system/appversion');
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '添加失败';
                $mssage['status'] = 0;
                $mssage['url']	  = site_url('system/appversion');
                echo json_encode($mssage);exit;
            }
        }
        $this->template->assign('segment_p','appversion');
        $this->template->load('system/appversion_add');
    }
    
    function appversion_edit(){
        if($_POST){
            $id = $_POST['id'];
            $data = array();
            
            $data['type'] = $_POST['type'];
             $data['app_type'] = $_POST['app_type'];
            $data['version'] = $_POST['version'];
            $data['url'] = $_POST['url'];
            $data['isupdate'] = $_POST['isupdate'];
            $data['content'] = $_POST['content'];
            
            if(!$data['app_type']){
                $mssage['info']	  = '请选择【应用App类型】';
                $mssage['status'] = 0;
                $mssage['url']	  = "";
                echo json_encode($mssage);exit;
            }
            if(!$data['type']){
                $mssage['info']	  = '请选择【操作系统类型】';
                $mssage['status'] = 0;
                $mssage['url']	  = "";
                echo json_encode($mssage);exit;
            }
            
            if($this->db->where(array("id"=>$id))->update("tan_version",$data)){
                $mssage['info']	  = '更新成功';
                $mssage['status'] = 1;
                $mssage['url']	  = site_url('system/appversion');
                echo json_encode($mssage);exit;
            }else{
                $mssage['info']	  = '更新失败';
                $mssage['status'] = 0;
                $mssage['url']	  = site_url('system/appversion');
                echo json_encode($mssage);exit;
            }
        }
        $id = $_GET['id'];
        $item = $this->db->query("select * from tan_version where id = $id order by id desc limit 1")->row_array();
        if(!$item){
            redirect("system/appversion");
        }
        $this->template->assign('item',$item);
        $this->template->assign('segment_p','appversion');
        $this->template->load('system/appversion_edit');
    }
}
